package com.ruoyi.partybuilding.common;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import lombok.var;
import org.springframework.core.ResolvableType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;


import javax.annotation.Resource;
import java.util.List;

public class EQueryService<T> {

    @Resource
    protected EQueryHelper queryHelper;
    final protected Class<?> tClass= ResolvableType.forClass(getClass()).getSuperType().getGeneric(0).resolve();

    public R<Page<T>> query(T entity, PageDomain pageRequest) {
        var pData = queryHelper.queryable(tClass)
                .whereObject(entity!=null,entity)
                .toPageResult(pageRequest.getPageNum(), pageRequest.getPageSize());
        var page = new PageImpl<>((List<T>)pData.getData(), Pageable.unpaged(),pData.getTotal());
        return R.ok(page);
    }

    public R<Long> add(T entity){
        var re= queryHelper.query().insertable(entity).executeRows();
        return R.ok(re);
    }

    public R<Long> remove(T entity){
        var re= queryHelper.query().deletable(entity).disableLogicDelete().allowDeleteStatement(true).executeRows();
        return R.ok(re);
    }

    public  R<Long> edit(T entity){
        var re= queryHelper.query().updatable(entity).executeRows();
        return  R.ok(re);
    }
}
